package week4.day22;

import java.io.*;

/**
 * @Author fengzihao
 * @Date 2024-05-06 20:38
 */
public class Copy {
    public static File[] pathName = new File[1000];
    public static int count = 0;
    public static void main(String[] args) {
        // File targetFile = new File("txt\\"+);
        findFile(new File("G:\\Java\\home-work\\src\\week2\\day8"));
        System.out.println(count);
        for (int i = 0; i < count; i++) {
            String name = "G:\\Java\\home-work\\java\\" + pathName[i].getName();
            File targetFile = new File(name);
            copy(pathName[i],targetFile);
        }
    }
    public static boolean copy(File copyFile,File targetFile){
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(copyFile),2048);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFile,true),2048);
            byte[] bytes = new byte[2048];
            bufferedInputStream.read(bytes);
            bufferedOutputStream.write(bytes);
            bufferedInputStream.close();
            bufferedOutputStream.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return true;
    }
    public static boolean findFile(File file) {
        File[] files = file.listFiles();
        if (files == null&&file.getName().endsWith(".java")){
            pathName[count++] = file;
            return true;
        }
        if (files.length == 0)
            return true;
        else {
            for (int i = 0; i < files.length; i++) {
                findFile(files[i]);
            }
        }
        return true;
    }
}
